#include<cstdio>
#include<algorithm>

using namespace std;
const int maxn=2e5+10;
int p[maxn];
int left=maxn,right=maxn;


int main()
{
	int n,b;
	char a;
	scanf("%d",&n);
	getchar();
	scanf("%c",&a);
	scanf("%d",&b);
	p[b]=maxn;
	getchar();
	for(int i=1;i<n;i++){
		scanf("%c",&a);
		scanf("%d",&b);
		if(a=='L') p[b]=--left;
		else if(a=='R') p[b]=++right;
		else{
			int sum=min(abs(p[b]-left),abs(p[b]-right));
			printf("%d\n",sum);
		}
		getchar();
	}
	return 0;
}
